package jh.com.jhappbf.assay;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.util.AbDialogUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.utils.JsonHelper;
import jh.com.jhappbf.weight.query.SelectActivity;

public class AssayAddMain extends AbActivity implements OnClickListener {

    private EditText mPlateNum;
    private ImageView mSearchImg;
    private ListView mListView;
    private List<JSONObject> mData;
    MyAssayAdapter mAdapter;
    private final int GET_PLATENUM_REQUEST_CODE = 0X0001;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.assayadd_main);
        AbTitleBar mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("化验添加");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        initView();
    }

    private void initView() {
        mPlateNum = findViewById(R.id.assay_add_search_edit);
        mSearchImg = findViewById(R.id.assay_add_search_platenum);
        mListView = findViewById(R.id.assay_add_main);
        mSearchImg.setOnClickListener(this);
        mAdapter = new MyAssayAdapter();
        mListView.setOnItemClickListener((arg0, view, position, arg3) -> {
            Intent intent = new Intent(AssayAddMain.this, AssayAdd.class);
            intent.putExtra("plateNum", mData.get(position).optString("车号"));
            intent.putExtra("assayId", mData.get(position).optString("编号"));
            intent.putExtra("mao", mData.get(position).optString("毛"));
            intent.putExtra("pi", mData.get(position).optString("皮"));

            startActivity(intent);
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.assay_add_search_platenum:
                selectPlateNum();
                break;
            case R.id.assay_btn_refresh:
                //refreshInput();
                break;
            case R.id.assay_search_platenum:
//			selectPlateNum();
                //	getFieldValue("platenumber",GET_PLATENUM_REQUEST_CODE);
                break;
            default:
                break;
        }
    }

    private void selectPlateNum() {
//		Intent i = new Intent(AssayAddMain.this,AssayPlatenumSelect.class);
        Intent i = new Intent(AssayAddMain.this, SelectActivity.class);
        i.putExtra("flag", "platenumber");
        startActivityForResult(i, GET_PLATENUM_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GET_PLATENUM_REQUEST_CODE && resultCode == 1) {
//			if(mData!=null){
//				mData.clear();
//				mAdapter.notifyDataSetChanged();
//			}


            String plateNum = data.getStringExtra("result");
            mPlateNum.setText(plateNum);
            getNoAssayPlateNum(plateNum);

        } else {
            getNoAssayPlateNum(mPlateNum.getText().toString());
        }


    }

    /**
     * 获取未化验的车号
     *
     */

    private void getNoAssayPlateNum(String plateNum) {
        ArrayList<String> param = new ArrayList<>();
        ArrayList<String> value = new ArrayList<>();
        String methodName = "getNoAssayPlatenum";
        param.add("plateNum");
        value.add(plateNum);

        Listener<String> listener = response -> {

            AbDialogUtil.removeDialog(AssayAddMain.this);
//				mRefreshView.onHeaderRefreshFinish();
//				mRefreshView.onFooterLoadFinish();


//				Log.e("wwh", response);
            List<JSONObject> newListData = JsonHelper
                    .getJsonObjects(response);
            if (newListData != null) {
                mData = newListData;
                mListView.setAdapter(mAdapter);
                //	mAdapter.notifyDataSetChanged();


            }
        };

        ErrorListener errorListener = error -> {
            AbDialogUtil.removeDialog(AssayAddMain.this);
//				mRefreshView.onHeaderRefreshFinish();
//				mRefreshView.onFooterLoadFinish();
//				setAbContentView(mErrorView);
//				mData.clear();
//				mAdapter.notifyDataSetChanged();
        };

        VolleyCallWebService webService = new VolleyCallWebService(methodName,
                param, value, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(AssayAddMain.this).addRequest(webService);


    }

    @Override
    protected void onResume() {
        super.onResume();
//        Log.e("wwh", mPlateNum.getText().toString());
        getNoAssayPlateNum(mPlateNum.getText().toString());//刷新未化验车号

    }

    class MyAssayAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return mData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                holder = new ViewHolder();
                convertView = View.inflate(AssayAddMain.this, R.layout.item_assay_main, null);
                holder.plateNum = convertView.findViewById(R.id.assay_platenum);
                holder.assayId = convertView.findViewById(R.id.assay_id);
                holder.mao = convertView.findViewById(R.id.assay_mao);
                holder.pi = convertView.findViewById(R.id.assay_pi);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            if (holder != null) {
                JSONObject obj = mData.get(position);
                holder.plateNum.setText("车号:" + obj.optString("车号"));
                holder.assayId.setText("编号:" + obj.optString("编号"));
                holder.mao.setText("毛重:" + obj.optString("毛"));
                holder.pi.setText("皮重:" + obj.optString("皮"));
            }
            return convertView;
        }
    }

    static class ViewHolder {
        TextView plateNum;
        TextView assayId;
        TextView mao;
        TextView pi;
    }

}
